<template>
  <el-form
    ref="loginFormRef"
    :model="loginForm"
    :rules="loginRules"
    class="login-form"
    @submit.prevent="handleLogin"
  >
    <el-form-item prop="username">
      <el-input
        v-model="loginForm.username"
        placeholder="用户名/邮箱/手机号"
        :size="compact ? 'default' : 'large'"
        prefix-icon="User"
        clearable
      />
    </el-form-item>

    <el-form-item prop="password">
      <el-input
        v-model="loginForm.password"
        type="password"
        placeholder="密码"
        :size="compact ? 'default' : 'large'"
        prefix-icon="Lock"
        show-password
        clearable
        @keyup.enter="handleLogin"
      />
    </el-form-item>

    <div class="form-options">
      <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
      <router-link to="/auth/forgot-password" class="forgot-link">忘记密码？</router-link>
    </div>

    <el-form-item>
      <el-button
        type="primary"
        :size="compact ? 'default' : 'large'"
        :loading="authStore.isLoading"
        class="login-button"
        @click="handleLogin"
      >
        {{ authStore.isLoading ? '登录中...' : '登录' }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'

const props = defineProps<{ compact?: boolean }>()
const emit = defineEmits<{ (e: 'success'): void }>()

const authStore = useAuthStore()
const loginFormRef = ref()
const loginForm = ref<{ username: string; password: string; rememberMe: boolean }>({
  username: '',
  password: '',
  rememberMe: false,
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate()
  const result = await authStore.login({
    username: loginForm.value.username,
    password: loginForm.value.password,
  } as any)
  if (result.success) emit('success')
}
</script>

<style scoped>
.login-form {
  width: 100%;
  max-width: 400px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #66b1ff;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 1.1rem;
  background: linear-gradient(135deg, #ff5a00 0%, #ff0036 100%);
  border: none;
  border-radius: 0;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .login-form {
    max-width: 100%;
  }
  
  .login-button {
    height: 44px;
    font-size: 1rem;
  }
  
  .form-options {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .forgot-link {
    font-size: 14px;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .login-button {
    transition: all 0.2s ease;
  }
  
  .login-button:active {
    transform: scale(0.98);
  }
  
  .el-input {
    transition: all 0.2s ease;
  }
  
  .el-input:focus-within {
    transform: scale(1.02);
  }
}

/* 小屏幕优化 */
@media (max-width: 480px) {
  .login-button {
    height: 40px;
    font-size: 0.95rem;
  }
  
  .el-form-item {
    margin-bottom: 16px;
  }
}
</style>

